<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="../../statics/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../../statics/css/font.css">
    <link rel="stylesheet" href="../../statics/css/xadmin.css">
    <link rel="stylesheet" href="../../css/metroStyle/metroStyle.css">
    <script src="../../statics/js/jquery.min.js"></script>
    <script src="../../js/jquery.ztree.all.min.js"></script>
    <script type="text/javascript" src="../../statics/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../statics/js/xadmin.js"></script>

</head>

<body>
<div class="x-body">
    <form class="layui-form">

        <div class="layui-form-item" style="display: none">
            <label for="id" class="layui-form-label">
                <span class="x-red">*</span>id
            </label>
            <div class="layui-input-inline">
                <input type="text" id="id" name="id" required
                       autocomplete="off" value="" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>


        <div class="layui-form-item">
            <label for="roleName" class="layui-form-label">
                <span class="x-red">*</span>角色名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="roleName" name="roleName"
                       autocomplete="off" value="" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label for="roleDesc" class="layui-form-label">
                <span class="x-red">*</span>角色描述
            </label>
            <div class="layui-input-inline">
                <textarea type="text" value="" id="roleDesc" name="roleDesc" style="width: 400px;height: 150px"
                          autocomplete="off" class="layui-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="roleDesc" class="layui-form-label">
                <span class="x-red">*</span>权限分配
            </label>
            <div class="layui-input-inline">
                <ul id="treeDemo" class="ztree" style="font-size: 50px"></ul>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="modify" lay-submit="">
                提交修改
            </button>
        </div>
    </form>
</div>
<script>
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        check: {

            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "ps", "N": "s"}
        },
        data: {
            key: {
                name: "permitionName"
            }
        }
    };

    setTimeout(function () {
        $.ajax({
            url: '/permition/getAllPermition',
            success: function (result) {
                if (0 == result.code) {
                    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
                    zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, result.data);
                }
                $.ajax({
                    url: "/permition/getPermitionByRoleId",
                    data: {
                        roleId: $("#id").val()
                    },
                    success: function (result) {
                        if (0 === result.code) {
                            //先获取所有的根节点
                            var nodes = zTreeObj.transformToArray(zTreeObj.getNodes());
                            for (var i = 0; i < nodes.length; i++) {
                                //判断当前节点编号是否在  角色对应的权限编号数组中，说明该选中，否则不选中
                                if ($.inArray(nodes[i].id, result.data) !== -1) {
                                    zTreeObj.checkNode(nodes[i], true, true);
                                } else {
                                    zTreeObj.checkNode(nodes[i], false, true);
                                }
                            }
                        }
                    }
                })
            }
        });
    }, 200);

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;
        //监听提交
        form.on('submit(modify)', function (data) {
            var nodes = zTreeObj.getCheckedNodes(true);
            var str = "";
            for (var i = 0; i < nodes.length; i++) {
                str += nodes[i].id + ",";
            }
            str = str.substring(0, str.length - 1);
            $.ajax({
                type: "post",
                url: "/role/modifyRoleInfoById",
                data: {
                    id: data.field.id,
                    roleName: data.field.roleName,
                    roleDesc: data.field.roleDesc,
                    permitionIds: str
                },
                success: function (result) {
                    if (0 == result.code) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(result.msg, {icon: 5, time: 1000});
                    }
                }
            });
            return false;
        });
    });
</script>
</body>

</html>